package com.daoeping.action;

import java.sql.Timestamp;
import java.util.List;


import com.daoeping.data.Bzn_answercomm;
import com.daoeping.data.Bzn_question;
import com.daoeping.data.Bzn_questionassigned;
import com.daoeping.data.Bzn_questionfollow;
import com.daoeping.domain.JsonData;
import com.daoeping.domain.Page;
import com.daoeping.domain.TableData;
import com.daoeping.data.Usr_user;
import com.daoeping.service.Bzn_answerService;
import com.daoeping.service.Bzn_answercommService;
import com.daoeping.service.Bzn_questionService;
import com.daoeping.service.Bzn_questionassignedService;
import com.daoeping.service.Bzn_questioncommService;
import com.daoeping.service.Bzn_questionfavoriteService;
import com.daoeping.service.Bzn_questionfollowService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Bzn_questionAction extends ActionSupport{
	private Bzn_questionService bzn_questionService;
	private Bzn_questionfollowService bzn_questionfollowService;
	private Bzn_questionassignedService bzn_questionassignedService;
	private Bzn_questionfavoriteService bzn_questionfavoriteService;
	private Bzn_answerService bzn_answerService;
	private Bzn_questioncommService bzn_questioncommService;
	private Bzn_answercommService bzn_answercommService;
	private Bzn_question bzn_question;
	private JsonData jsonData;
	private Page page;
	private TableData tableData;
	

	public Bzn_answercommService getBzn_answercommService() {
		return bzn_answercommService;
	}

	public void setBzn_answercommService(Bzn_answercommService bzn_answercommService) {
		this.bzn_answercommService = bzn_answercommService;
	}

	public Bzn_questionfavoriteService getBzn_questionfavoriteService() {
		return bzn_questionfavoriteService;
	}

	public void setBzn_questionfavoriteService(
			Bzn_questionfavoriteService bzn_questionfavoriteService) {
		this.bzn_questionfavoriteService = bzn_questionfavoriteService;
	}

	public Bzn_answerService getBzn_answerService() {
		return bzn_answerService;
	}

	public void setBzn_answerService(Bzn_answerService bzn_answerService) {
		this.bzn_answerService = bzn_answerService;
	}

	public Bzn_questioncommService getBzn_questioncommService() {
		return bzn_questioncommService;
	}

	public void setBzn_questioncommService(
			Bzn_questioncommService bzn_questioncommService) {
		this.bzn_questioncommService = bzn_questioncommService;
	}

	public Bzn_questionassignedService getBzn_questionassignedService() {
		return bzn_questionassignedService;
	}

	public void setBzn_questionassignedService(
			Bzn_questionassignedService bzn_questionassignedService) {
		this.bzn_questionassignedService = bzn_questionassignedService;
	}

	public Bzn_questionfollowService getBzn_questionfollowService() {
		return bzn_questionfollowService;
	}

	public void setBzn_questionfollowService(
			Bzn_questionfollowService bzn_questionfollowService) {
		this.bzn_questionfollowService = bzn_questionfollowService;
	}

	public JsonData getJsonData() {
		return jsonData;
	}

	public void setJsonData(JsonData jsonData) {
		this.jsonData = jsonData;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public TableData getTableData() {
		return tableData;
	}

	public void setTableData(TableData tableData) {
		this.tableData = tableData;
	}

	public Bzn_question getBzn_question() {
		return bzn_question;
	}

	public void setBzn_question(Bzn_question bzn_question) {
		this.bzn_question = bzn_question;
	}

	public Bzn_questionService getBzn_questionService() {
		return bzn_questionService;
	}

	public void setBzn_questionService(Bzn_questionService bzn_questionService) {
		this.bzn_questionService = bzn_questionService;
	}

	public String addBzn_question(){
		bzn_question.setCreateDatetime(new Timestamp(System.currentTimeMillis()));
		Bzn_questionfollow bzn_questionfollow = new Bzn_questionfollow();
		bzn_questionfollow.setBzn_question(bzn_question);
		bzn_questionfollow.setUsr_user((Usr_user)ActionContext.getContext().getSession().get("user"));
		if(bzn_questionService.addBzn_question(bzn_question)){
			//add follow 
			if(bzn_questionfollowService.addBzn_questionfollow(bzn_questionfollow)){
				String [] strArr = jsonData.getJsonData().split(",");
				if(!jsonData.getJsonData().equals("")){
					for (int i = 0; i < strArr.length; i++) {
						// add question assigned
						Usr_user usr_user = new Usr_user();
						usr_user.setId(Integer.parseInt(strArr[i]));
						Bzn_questionassigned bzn_questionassigned = new Bzn_questionassigned();
						bzn_questionassigned.setBzn_question(bzn_question);
						bzn_questionassigned.setUsr_user(usr_user);
						bzn_questionassigned.setCreateDatetime(new Timestamp(System.currentTimeMillis()));
						if(!bzn_questionassignedService.addBzn_questionassigned(bzn_questionassigned)){
							return "exception";
						}
					}
				}
				return "addBzn_question";
			}else{
				return "exception";
			}
		}else{
			return "exception";
		}
	}
	
	public String findAllBzn_questionByPage(){
		jsonData = new JsonData();
		bzn_question.setUsr_user((Usr_user)ActionContext.getContext().getSession().get("user"));
		jsonData = bzn_questionService.findAllBzn_questionByPage(page, tableData , bzn_question);
		return "findAllBzn_questionByPage";
	
	}
	
	public String findBzn_questionById(){
		bzn_question = bzn_questionService.findBzn_questionById(bzn_question);
		if(bzn_question==null){
			return "exception";
		}else{
			//draft
			if(bzn_question.getFlag()==1){
				Bzn_questionassigned bzn_questionassigned = new Bzn_questionassigned();
				bzn_questionassigned.setBzn_question(bzn_question);
				List <Bzn_questionassigned> bzn_questionassignedList = bzn_questionassignedService.findBzn_questionassignedList(bzn_questionassigned);
				String str = "";
				for (Bzn_questionassigned newbzn_questionassigned : bzn_questionassignedList) {
					str = str + newbzn_questionassigned.getUsr_user().getUsername()+"<"+newbzn_questionassigned.getUsr_user().getId()+",";
				}
				ActionContext.getContext().getSession().put("questionassigned", str);
			}
			ActionContext.getContext().getSession().put("question", bzn_question);
			Bzn_questionfollow bzn_questionfollow = new Bzn_questionfollow();
			bzn_questionfollow.setUsr_user((Usr_user)ActionContext.getContext().getSession().get("user"));
			bzn_questionfollow.setBzn_question(bzn_question);
			bzn_questionfollow = bzn_questionfollowService.findBzn_questionfollowById(bzn_questionfollow);
			if(bzn_questionfollow==null){
				//add
				ActionContext.getContext().getSession().put("follow", "");
			}else{
				//delete
				ActionContext.getContext().getSession().put("follow", bzn_questionfollow.getId());
			}
			if(bzn_question.getFlag()==0){
				return "question";
			}else if(bzn_question.getFlag()==1){
				return "questiondraft";
			}else{
				return "question";
			}
		}
	}
	
	public String editBzn_question(){
		bzn_question.setUpdateDatetime(new Timestamp(System.currentTimeMillis()));
		bzn_question.setCreateDatetime(((Bzn_question)ActionContext.getContext().getSession().get("question")).getCreateDatetime());
		if(bzn_questionService.editBzn_question(bzn_question)){
				if(jsonData!=null){
				
				String strJson = jsonData.getJsonData();
				String strSession = ActionContext.getContext().getSession().get("questionassigned")+"";
				String [] strArrAdd = (String [])this.findDiffString(strSession, strJson , "add");
				String  strDel = (String)this.findDiffString(strSession, strJson , "del");
				if(!strDel.equals("")){
					//del
					jsonData.setJsonData(strDel.substring(0,strDel.length()-1));
					Bzn_questionassigned bzn_questionassigned = new Bzn_questionassigned();
					bzn_questionassigned.setBzn_question(bzn_question);
					if(!bzn_questionassignedService.delBzn_questionassigned(jsonData , bzn_questionassigned)){
						return "exception";
					}
				}
				System.out.println(strArrAdd.length);
				if(!strArrAdd[0].equals("")){
					for (int i = 0; i < strArrAdd.length; i++) {
						// add question assigned
						Usr_user usr_user = new Usr_user();
						System.out.println(strArrAdd[i]);
						usr_user.setId(Integer.parseInt(strArrAdd[i]));
						Bzn_questionassigned bzn_questionassigned = new Bzn_questionassigned();
						bzn_questionassigned.setBzn_question(bzn_question);
						bzn_questionassigned.setUsr_user(usr_user);
						bzn_questionassigned.setCreateDatetime(new Timestamp(System.currentTimeMillis()));
						if(!bzn_questionassignedService.addBzn_questionassigned(bzn_questionassigned)){
							return "exception";
						}
					}
				}
				
				Bzn_questionassigned bzn_questionassigned = new Bzn_questionassigned();
				bzn_questionassigned.setBzn_question(bzn_question);
				List <Bzn_questionassigned> bzn_questionassignedList = bzn_questionassignedService.findBzn_questionassignedList(bzn_questionassigned);
				String str = "";
				for (Bzn_questionassigned newbzn_questionassigned : bzn_questionassignedList) {
					str = str + newbzn_questionassigned.getUsr_user().getUsername()+"<"+newbzn_questionassigned.getUsr_user().getId()+",";
				}
				ActionContext.getContext().getSession().put("questionassigned", str);
				
			}ActionContext.getContext().getSession().put("question", bzn_question);
			
			if(bzn_question.getFlag()==0&&page.getFlag().equals("myquestion")){
				return "edit_myquestion";
			}else if(bzn_question.getFlag()==1){
				return "edit_questiondraft";
			}else{
				return "edit_question";
			}
		}else{
			return "exception";
		}
	}
	
	
	
	public String delBzn_question(){
		
		if(bzn_answercommService.delBzn_answercommByQuestionId(bzn_question)&&bzn_questionassignedService.delBzn_questionassignedByQuestionId(bzn_question)&&bzn_questioncommService.delBzn_questioncommByQuestionId(bzn_question)&&bzn_answerService.delBzn_answerByQuestionId(bzn_question)&&bzn_questionfavoriteService.delBzn_questionfavoriteByQuestionId(bzn_question)&&bzn_questionfollowService.delBzn_questionfollowByQuestionId(bzn_question)&&bzn_questionService.delBzn_question(bzn_question)){
			if(bzn_question.getFlag()==0&&page.getFlag()!=null&&!page.getFlag().equals("")&&page.getFlag().equals("myquestion")){
				return "del_myquestion";
			}else if(bzn_question.getFlag()==1){
				return "del_questiondraft";
			}else{
				return "del_question";
			}
		}else{
			return "exception";
		}
	}
	
	
	
	
	//str1 : name<id,name<id,   str2 id,id,
	public Object  findDiffString(String str1 , String str2 , String flag ){
		if(str1.equals("")||str2.equals("")){
			if(flag.equals("del")){
				String str = "";
				if(!str1.equals("")){
					String [] strarr = str1.split(",");
					for (int i = 0; i < strarr.length; i++) {
						str = str + strarr[i].split("<")[1] + ",";
					}
				}else{
					str = str1 ;
				}
				return str;
			}else if(flag.equals("add")){
				return str2.split(",");
			}
		}
		String [] sArray = str1.split(",");
		String newStr1 = "";
		for (int i = 0; i < sArray.length; i++) {
			newStr1 = newStr1 + sArray[i].split("<")[1]+",";
		}
		String []strarr = newStr1.split(",");
		String []strarr2 = str2.split(",");
		newStr1 = "";
		if(flag.equals("add")){
			for (int i = 0; i < strarr2.length; i++) {
				int count = 0 ;
				for (int j = 0; j < strarr.length; j++) {
					if(strarr2[i].equals(strarr[j])){
						count++;
					}
				}
				if(count==0){
					newStr1 = newStr1 + strarr2[i] + ",";
				}
			}
			System.out.println(newStr1);
			return newStr1.split(",");
		}else if(flag.equals("del")){
			for (int i = 0; i < strarr.length; i++) {
				int count = 0 ;
				for (int j = 0; j < strarr2.length; j++) {
					if(strarr[i].equals(strarr2[j])){
						count++;
					}
				}
				if(count==0){
					newStr1 = newStr1 + strarr[i] + ",";
				}
			}
			return newStr1;
		}
		return null;
	}
	

	
	
	//test
	public String login(){
		Usr_user usr_user = new Usr_user();
		usr_user.setId(1);
		ActionContext.getContext().getSession().put("user", usr_user);
		Bzn_answercomm bzn_answercomm = new Bzn_answercomm();
		bzn_answercomm.setUsr_user(usr_user);
	    List<Bzn_answercomm> bzn_answercommList = bzn_answercommService.findBzn_answercommList(bzn_answercomm);
		ActionContext.getContext().getSession().put("bzn_answercommList", bzn_answercommList);
	    System.out.println(bzn_answercommList);
	    System.out.println(((Usr_user)ActionContext.getContext().getSession().get("user")).getId());
	    return "login";
	}
	public String login2(){
		Usr_user usr_user = new Usr_user();
		usr_user.setId(2);
		ActionContext.getContext().getSession().put("user", usr_user);
		Bzn_answercomm bzn_answercomm = new Bzn_answercomm();
		bzn_answercomm.setUsr_user(usr_user);
	    List<Bzn_answercomm> bzn_answercommList = bzn_answercommService.findBzn_answercommList(bzn_answercomm);
		ActionContext.getContext().getSession().put("bzn_answercommList", bzn_answercommList);
	    System.out.println(bzn_answercommList);
	    System.out.println(((Usr_user)ActionContext.getContext().getSession().get("user")).getId());
		return "login";
	}
	
	
	
	
	public static void main(String[] args) {
	//	System.out.println(new Bzn_questionAction().findDiffString("sa<2,sa<3,", "3,4" , "add"));
		System.out.println("".split(",")[0].equals(""));
	}
}
